package com.deepexp.equip;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by yanceywang on 3/27/16.
 */
public class RetrofitUtil {

    //    //    //内网
//    public static final String HOST = String.format("http://%s:%s/", Global.ip, Global.port);
//    外网
//    public static final String HOST = String.format("https://%s/", Global.ip);
    private static Retrofit retrofit;
    private static ParkService apiService;
    private static Context mContext;
    private static RetrofitUtil sNewInstance = null;

    public static RetrofitUtil getInstance(Context context) {
        if (context != null) {
            mContext = context;
        }
        if (sNewInstance == null) {
            // 采用双重检查加锁实例化单件(单例锁模式)
            synchronized (RetrofitUtil.class) {
                if (sNewInstance == null) {
                    sNewInstance = new RetrofitUtil(
                            mContext, -1);
                }
            }
        }
        return sNewInstance;
    }

    private RetrofitUtil(Context context, int TimeOut) {

        this(context, null, TimeOut);
    }

    private RetrofitUtil(Context context, String url, int TimeOut) {
        LogInterceptor fullLoggingInterceptor = new LogInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("OKHttp", message);
            }
        });
//        fullLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//        if (TEST_MODE) {
//            builder.addInterceptor(new OkHttpProfilerInterceptor());
//        }
        builder.retryOnConnectionFailure(false)
                .addInterceptor(fullLoggingInterceptor)
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                //以下验证不设置，那么默认就已经设置了验证
//                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
                .hostnameVerifier(SSLSocketClient.getHostnameVerifier());
//                .dns(new ApiDns());
        if (Build.VERSION.SDK_INT < 29) {
            builder.sslSocketFactory(SSLSocketClient.getSSLSocketFactory());
        }
        OkHttpClient httpClient = builder.build();

        // add your other interceptors …
        // add logging as last interceptor
//            if(Constants.TEST_MODE) {
//                httpClient.interceptors().add(logging);  // <-- this is the important line!
//            }else {
//                httpClient.interceptors().add(new MyInterceptor());
//            }
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:s").create();
        retrofit = new Retrofit.Builder().baseUrl("http://192.168.0.73:8080/")
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(httpClient).build();

        apiService = retrofit.create(ParkService.class);
    }

    public ParkService getService() {
        return apiService;
    }

}
